/**
 * This file is part of iSecurePasswords.
 *
 *   iSecurePasswords is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   iSecurePasswords is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with iSecurePasswords.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Author: Matthew MacGregor
 * Created: 2012
 * iSecurePasswords, copyright 2013 Sudolink, LLC
 * 
 */ 
package com.sudolink.isp.ui;

import com.sudolink.isp.app.Application;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JDialog;
import javax.swing.JOptionPane;

/**
 *
 * @author dev
 */
public class PasswordGenerator extends javax.swing.JDialog {

    PasswordGeneratorManager controller;
    private char[] password;
    private boolean isNewPassword;
    
    public char[] getPassword() {
        return password;
    }
    /**
     * Creates new form PasswordGenerator
     */
    public PasswordGenerator(  ) {
        super( (JDialog)null, true );
        initComponents();
        Application.refreshLastUserInteractionTime();
        pwPassword.setEchoChar('*');
        isNewPassword = false;
        controller = new PasswordGeneratorManager();
        
        
        addWindowListener(new WindowAdapter() {
            
            @Override
            public void windowClosing(WindowEvent e){
                    PasswordGenerator.this.dispose();
            }
        
        });
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        cbSpecialCharacters = new javax.swing.JCheckBox();
        cbLetterCase = new javax.swing.JComboBox();
        panelPasswordLength = new javax.swing.JPanel();
        lblPasswordLength = new javax.swing.JLabel();
        tbPasswordLength = new javax.swing.JTextField();
        btnGeneratePassword = new javax.swing.JButton();
        pwPassword = new javax.swing.JPasswordField();
        cbShowPassword = new javax.swing.JCheckBox();
        btnAcceptPassword = new javax.swing.JButton();
        cbNumericCharacters = new javax.swing.JCheckBox();

        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        setResizable(false);

        cbSpecialCharacters.setSelected(true);
        cbSpecialCharacters.setText("Use Special Characters");

        cbLetterCase.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Upper and Lower Case", "Lower Case Only", "Upper Case Only" }));

        lblPasswordLength.setText("Password Length");

        tbPasswordLength.setText("15");

        btnGeneratePassword.setText("Generate");
        btnGeneratePassword.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGeneratePasswordActionPerformed(evt);
            }
        });

        cbShowPassword.setText("Show");
        cbShowPassword.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbShowPasswordActionPerformed(evt);
            }
        });

        btnAcceptPassword.setText("Accept");
        btnAcceptPassword.setEnabled(false);
        btnAcceptPassword.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAcceptPasswordActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panelPasswordLengthLayout = new javax.swing.GroupLayout(panelPasswordLength);
        panelPasswordLength.setLayout(panelPasswordLengthLayout);
        panelPasswordLengthLayout.setHorizontalGroup(
            panelPasswordLengthLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelPasswordLengthLayout.createSequentialGroup()
                .addGroup(panelPasswordLengthLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelPasswordLengthLayout.createSequentialGroup()
                        .addComponent(tbPasswordLength, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblPasswordLength, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(panelPasswordLengthLayout.createSequentialGroup()
                        .addComponent(pwPassword, javax.swing.GroupLayout.PREFERRED_SIZE, 206, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cbShowPassword)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 36, Short.MAX_VALUE)
                .addGroup(panelPasswordLengthLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(btnAcceptPassword, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnGeneratePassword, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );
        panelPasswordLengthLayout.setVerticalGroup(
            panelPasswordLengthLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelPasswordLengthLayout.createSequentialGroup()
                .addGroup(panelPasswordLengthLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tbPasswordLength, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblPasswordLength)
                    .addComponent(btnGeneratePassword))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(panelPasswordLengthLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(pwPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbShowPassword)
                    .addComponent(btnAcceptPassword))
                .addContainerGap())
        );

        cbNumericCharacters.setSelected(true);
        cbNumericCharacters.setText("Use Numeric Characters");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(panelPasswordLength, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(cbSpecialCharacters)
                        .addGap(19, 19, 19)
                        .addComponent(cbNumericCharacters)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(cbLetterCase, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbSpecialCharacters)
                    .addComponent(cbNumericCharacters))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cbLetterCase, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panelPasswordLength, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnGeneratePasswordActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGeneratePasswordActionPerformed
        if ( cbNumericCharacters.isSelected() ) {
            controller.setIsUsingNumericCharacters(true);
        } else {
            controller.setIsUsingNumericCharacters(false);
        }
        if ( cbSpecialCharacters.isSelected() ) {
            controller.setIsUsingSpecialCharacters(true);
        } else {
            controller.setIsUsingSpecialCharacters(false);
        }
        String characterCases = (String) cbLetterCase.getSelectedItem();
        switch (characterCases) {
            case "Upper and Lower Case":
                controller.setIsUsingLowerCaseCharacters(true);
                controller.setIsUsingUpperCaseCharacters(true);
                break;
            case "Lower Case Only":
                controller.setIsUsingLowerCaseCharacters(true);
                controller.setIsUsingUpperCaseCharacters(false);
                break;
            case "Upper Case Only":
                controller.setIsUsingLowerCaseCharacters(false);
                controller.setIsUsingUpperCaseCharacters(true);
                break;
            default:
                controller.setIsUsingLowerCaseCharacters(true);
                controller.setIsUsingUpperCaseCharacters(true);
                break;
        }
        
        int length = Integer.parseInt(tbPasswordLength.getText());
        password = controller.generatePassword(length);
        pwPassword.setText(new String(password));
        btnAcceptPassword.setEnabled(true);


    }//GEN-LAST:event_btnGeneratePasswordActionPerformed

    private void btnAcceptPasswordActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAcceptPasswordActionPerformed
        
        if ( isNewPassword == true || 
             JOptionPane.showConfirmDialog(this, 
             "This will remove your old password. Are you sure?") == JOptionPane.YES_OPTION ) {
            this.setVisible(false);
        }
            
        
    }//GEN-LAST:event_btnAcceptPasswordActionPerformed

    private void cbShowPasswordActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbShowPasswordActionPerformed
        if (cbShowPassword.isSelected() ) {
            pwPassword.setEchoChar( (char)0);
        } else {
            pwPassword.setEchoChar('*');
        }
    }//GEN-LAST:event_cbShowPasswordActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAcceptPassword;
    private javax.swing.JButton btnGeneratePassword;
    private javax.swing.JComboBox cbLetterCase;
    private javax.swing.JCheckBox cbNumericCharacters;
    private javax.swing.JCheckBox cbShowPassword;
    private javax.swing.JCheckBox cbSpecialCharacters;
    private javax.swing.JLabel lblPasswordLength;
    private javax.swing.JPanel panelPasswordLength;
    private javax.swing.JPasswordField pwPassword;
    private javax.swing.JTextField tbPasswordLength;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the isNewPassword
     */
    public boolean isNewPassword() {
        return isNewPassword;
    }

    /**
     * @param isNewPassword the isNewPassword to set
     */
    public void isNewPassword(boolean isNewPassword) {
        this.isNewPassword = isNewPassword;
    }
}
